package com.example.test;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;

public class MainActivity extends AppCompatActivity {

    private TextView test;
    private File sdDir;
    private BufferedReader reader;
    private EditText imei;
    private TelephonyManager service;
    private TextView imei2;
    private WifiManager wifiservice;
    private EditText mac;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //        应用程序申请获取Root权限
        upgradeRootPermission(getPackageCodePath());

        test = (TextView) findViewById(R.id.tv_test);
        imei = (EditText) findViewById(R.id.et_imei);
        imei2 = (TextView) findViewById(R.id.tv_imei2);
        mac = (EditText) findViewById(R.id.et_mac);

        service = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        wifiservice = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        //读取当前设备的IMEI
        Log.i("TTTTTTT","==============================================");
        test.setText("当前设备的IMEI:" + service.getDeviceId());
        Log.i("TTTTTTT","==============================================");
        findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setDate();
            }
        });

    }

    private void setDate() {
        try {
            SharedPreferences sh = this.getSharedPreferences("prefs", Context.MODE_WORLD_READABLE);
            SharedPreferences.Editor pre = sh.edit();
            pre.putString("imei", imei.getText().toString());
            pre.putString("mac", mac.getText().toString());
            pre.apply();
            Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /**
     * 应用程序运行命令获取 Root权限，设备必须已破解(获得ROOT权限)
     *
     * @return 应用程序是/否获取Root权限
     */
    public static boolean upgradeRootPermission(String pkgCodePath) {
        java.lang.Process process = null;
        DataOutputStream os = null;
        try {
            String cmd = "chmod 777 " + pkgCodePath;
            process = Runtime.getRuntime().exec("su"); //切换到root帐号
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                process.destroy();
            } catch (Exception e) {
            }
        }
        return true;
    }

}
